package com.sky.service.impl;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.AddressBook;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.entity.ShoppingCart;
import com.sky.exception.AddressBookBusinessException;
import com.sky.exception.ShoppingCartBusinessException;
import com.sky.mapper.AddressBookMapper;
import com.sky.mapper.UserOrderDetialMapper;
import com.sky.mapper.UserOrderMapper;
import com.sky.mapper.UserShopCarServiceMapper;
import com.sky.service.UserOrderService;
import com.sky.vo.OrderSubmitVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 符健清
 * @version 1.0
 */
@SuppressWarnings({"all"})
@Transactional
@Service
public class UserOrderSerivceImpl  implements UserOrderService {
    @Autowired
    private UserOrderMapper userOrderMapper;
    /**
     * 操作订单明细表
     * @param ordersSubmitDTO
     * @return
     */
    @Autowired
    private UserOrderDetialMapper userOrderDetialMapper;
    @Autowired
    private AddressBookMapper addressBookMapper;
    @Autowired
    private UserShopCarServiceMapper userShopCarServiceMapper;

    @Override
    public OrderSubmitVO sumbit(OrdersSubmitDTO ordersSubmitDTO) {
//      判断用户填写的地址  是否 null   购物车是否是null
        AddressBook addressBook = addressBookMapper.getById(ordersSubmitDTO.getAddressBookId());
        if (addressBook == null) {
//            抛出业务异常
            throw    new AddressBookBusinessException(MessageConstant.ADDRESS_BOOK_IS_NULL);
        }
//    查看购物车的数据
        Long  userId  = BaseContext.getCurrentId();
        List<ShoppingCart> shoppingCarts = userShopCarServiceMapper.selectShopping(userId);
        if(shoppingCarts ==null && shoppingCarts.size()==0){
//            抛出购物车数量异常
            throw new  ShoppingCartBusinessException(MessageConstant.SHOPPING_CART_IS_NULL);
        }
//    像订单插入一条数据
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersSubmitDTO, orders);
     orders.setOrderTime(LocalDateTime.now());
     orders.setPayStatus(Orders.UN_PAID );
     orders.setNumber(String.valueOf(System.currentTimeMillis()));
//     查询用户手机号
        orders.setPhone(addressBook.getPhone());
        orders.setConsignee(addressBook.getConsignee());
        orders.setUserId(BaseContext.getCurrentId());
//       添加数据
       userOrderDetialMapper.insert (orders);

//    向订单明细表插入n 条数据
//        读取遍历购物车的数据
     List <OrderDetail> lists =new ArrayList<>();
        for (ShoppingCart shoppingCart : shoppingCarts) {
      OrderDetail orderDetail = new OrderDetail();
      BeanUtils.copyProperties(shoppingCart, orderDetail);
      orderDetail.setOrderId(orders.getId());
      lists.add(orderDetail);
  }
//      插入订单明细表格
        userOrderDetialMapper.insertBath(lists);

//      下单成功清空购物车
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        userShopCarServiceMapper.deleteAllShoppingCar(shoppingCart);

//        封装 返回数据
        OrderSubmitVO  orderSubmitVO= OrderSubmitVO.builder()
                .id(orders.getId())
                .orderTime(orders.getOrderTime())
                .orderAmount(orders.getAmount())
                .orderNumber(orders.getNumber())
                .build();
        return   orderSubmitVO;
    }
}
